Imports AUtility

Partial Public Class SezonaLigaIgrac
    Inherits System.Web.UI.Page

    Dim selectStr As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            'My.Settings.ConnectionString = "Data Source=217.14.208.72;Initial Catalog=mojdb;Persist Security Info=True;User ID=user;Password=moj1247"
            FillControls()
            'GetTable(2, 1)
        End If
        GetIgraci()
    End Sub

    Private Sub FillControls()

        selectStr = String.Format("SELECT * FROM Sezona ORDER BY DatumOd DESC")
        ddlSezona.DataSource = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        ddlSezona.DataTextField = "SezonaNaziv"
        ddlSezona.DataValueField = "SezonaId"
        ddlSezona.DataBind()

        selectStr = String.Format("SELECT * FROM Liga ORDER BY LigaNaziv")
        ddlLiga.DataSource = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        ddlLiga.DataTextField = "LigaNaziv"
        ddlLiga.DataValueField = "LigaId"
        ddlLiga.DataBind()

        FillIgraci()

    

    End Sub

 
    Private Sub GetIgraci()

        selectStr = String.Format("SELECT * FROM vSezonaLigaIgrac WHERE SezonaId = {0} AND LigaId = {1}", ddlSezona.SelectedValue, ddlLiga.SelectedValue)
        gvIgraci.DataSource = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        gvIgraci.DataBind()

    End Sub

    Private Sub FillIgraci()
        selectStr = String.Format("SELECT * FROM vIgrac ORDER BY ImePrezime", ddlLiga.SelectedValue)
        Dim pobjednikDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        Dim porazeniDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)

        ddlIgrac.DataSource = pobjednikDT
        ddlIgrac.DataTextField = "ImePrezime"
        ddlIgrac.DataValueField = "IgracId"
        ddlIgrac.DataBind()

    End Sub

    Protected Sub btnSpremi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpremi.Click
        selectStr = String.Format("INSERT INTO SezonaLigaIgrac (SezonaId, LigaId, IgracId) VALUES ({0}, {1}, {2})", ddlSezona.SelectedValue, ddlLiga.SelectedValue, ddlIgrac.SelectedValue)
        DataAccess.ExecuteSql(GlobalTenisLiga.ConnectionString, selectStr)
        Dim ligaSession As String = ddlSezona.SelectedValue.ToString() + "_" + ddlLiga.SelectedValue.ToString()
        Application(ligaSession) = Nothing
        GetIgraci()
    End Sub

    Protected Sub gvIgraci_RowDeleting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvIgraci.RowDeleting
        Dim id As Integer = 0
        id = Convert.ToInt16(gvIgraci.DataKeys(e.RowIndex).Value.ToString())
        selectStr = String.Format("DELETE SezonaLigaIgrac WHERE SezonaLigaIgracId = {0}", id)
        DataAccess.ExecuteSql(GlobalTenisLiga.ConnectionString, selectStr)
        Dim ligaSession As String = ddlSezona.SelectedValue.ToString() + "_" + ddlLiga.SelectedValue.ToString()
        Application(ligaSession) = Nothing

        GetIgraci()
    End Sub
End Class